package se137.hrbust.hlgshopping137.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import se137.hrbust.hlgshopping137.entity.PCategroy137;
import se137.hrbust.hlgshopping137.entity.Product137;
import se137.hrbust.hlgshopping137.entity.Prosku137;
import se137.hrbust.hlgshopping137.service.PCategroyService137;
import se137.hrbust.hlgshopping137.service.Product137Service;
import se137.hrbust.hlgshopping137.mapper.Product137Mapper;
import org.springframework.stereotype.Service;
import se137.hrbust.hlgshopping137.service.Prosku137Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author ZhaoSY
* @description 针对表【tb_product137】的数据库操作Service实现
* @createDate 2025-09-11 10:50:00
*/
@Service
public class Product137ServiceImpl extends ServiceImpl<Product137Mapper, Product137>
    implements Product137Service{
 @Autowired
    Prosku137Service prosku137Service;
 @Autowired
 PCategroyService137 pCategoryService137;

    public Product137ServiceImpl(Prosku137Service prosku137Service) {
        this.prosku137Service = prosku137Service;
    }

    @Override
    public Map<String, Object> getProductDetail(Long pid) {
        Map<String, Object> result = new HashMap<>();

        // 获取商品基本信息
        Product137 product = this.getById(pid);
        if (product == null) {
            return null;
        }

        // 获取商品型号信息
        QueryWrapper<Prosku137> skuWrapper = new QueryWrapper<>();
        skuWrapper.eq("FPID", pid);
        List<Prosku137> skuList = prosku137Service.list(skuWrapper);

        // 获取商品分类信息

        PCategroy137 category = pCategoryService137.findCategroyById137(product.getPcatid());

        // 构建返回数据
        result.put("id", product.getPid());
        result.put("name", product.getPname137());
        result.put("desc", product.getPdetail137());
        result.put("price", skuList.isEmpty() ? 0 : skuList.get(0).getPricesell137());
        result.put("oldPrice", skuList.isEmpty() ? 0 : skuList.get(0).getPriceorigion());
        result.put("inventory", skuList.isEmpty() ? 0 : skuList.get(0).getStocknum());

        // 构建图片数组
        String[] images = {product.getPimage1(), product.getPimage2(), product.getPimage3(),
                product.getPimage4(), product.getPimage5()};
        result.put("mainPictures", images);

        // 构建规格信息
        result.put("specs", skuList);

        // 构建分类信息
        if (category != null) {
            Map<String, Object> categoryInfo = new HashMap<>();
            categoryInfo.put("id", category.getPCategroyID137());
            categoryInfo.put("name", category.getPCategroyName137());
            result.put("categories", categoryInfo);
        }

        return result;
    }


}




